from django.contrib import admin

# Register your models here.

from .models import Article, ArticleTags, ArticleCategory


class ArticleAdmin(admin.ModelAdmin):
    # 显示字段
    list_display = ('title', 'show_tags', 'author', 'description', 'create_date', 'update_time',)

    # raw_id_fields = ('tags',)  # 放大镜

    def show_tags(self, obj):
        return ",".join([tag.tag_name for tag in obj.tags.all()])

    show_tags.short_description = "Tags"

    filter_horizontal = ('tags',)  # 左右选框
    # list_display_links = ('title',)

    # 可编辑字段
    # list_editable = ("title",)

    # inlines = [ArticleTags]

    # 分页
    list_per_page = 50

    # 每页50条记录
    list_max_show_all = 50

    actions_on_top = True

    # 过滤选项
    list_filter = ('title', 'author', 'create_date', 'tags',)

    # 搜索框
    search_fields = ['title', 'content']


class ArticleTagsAdmin(admin.ModelAdmin):
    list_display = ('tag_name',)

    list_per_page = 50

    actions_on_top = True


class ArticleCategoryAdmin(admin.ModelAdmin):
    list_display = ('category_name',)

    list_per_page = 50

    actions_on_top = True


admin.site.register(Article, ArticleAdmin)
admin.site.register(ArticleTags, ArticleTagsAdmin)
admin.site.register(ArticleCategory, ArticleCategoryAdmin)
